package com.zmn.oms.business.impl.changerecord;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.changerecord.OrderChangeRecordBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.model.dto.changerecord.ChangeLiableMasterDTO;
import com.zmn.oms.model.entity.changerecord.OrderChangeRecord;
import com.zmn.oms.services.interfaces.changerecord.OrderChangeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 类描述 订单预约，派单等信息修改记录
 *
 * @author xiewenbing
 * @date 2019/11/26 15:20
 */
@Slf4j
@Service
public class OrderChangeRecordBServiceImpl implements OrderChangeRecordBService {

    @Resource
    private OrderChangeRecordService orderChangeRecordService;


    @Override
    public void updateLiableMasterRecord(ChangeLiableMasterDTO changeLiableMasterDTO) {
        if (Objects.isNull(changeLiableMasterDTO.getOrderId())) {
            return;
        }

        OrderChangeRecord orderChangeRecord = orderChangeRecordService.getChangeRecordByOrderId(changeLiableMasterDTO.getOrderId());
        if (Objects.isNull(orderChangeRecord)) {
            return;
        }

        // 原工程师判断：责任工程师是原工程师并且已存在，不做修改操作
        if (Objects.equals(OrderConsts.ORDER_LIABLE_TYPE_SOURCE_MASTER, changeLiableMasterDTO.getLiableType())
                && NumberUtil.isNotNullOrZero(orderChangeRecord.getLiableMasterId())) {
            return;
        }

        // 当前工程师或者 没有 责任工程师时保存责任工程师
        ChangeLiableMasterDTO updateChangeLiableMasterDTO = new ChangeLiableMasterDTO();
        updateChangeLiableMasterDTO.setOrderId(changeLiableMasterDTO.getOrderId());
        updateChangeLiableMasterDTO.setLiableMasterId(changeLiableMasterDTO.getMasterId());
        updateChangeLiableMasterDTO.setLiableMasterName(changeLiableMasterDTO.getMasterName());
        orderChangeRecordService.updateLiableMasterRecord(updateChangeLiableMasterDTO);
    }

    @Override
    public void updateCancelLiableMasterRecord(Long orderId,  Long workId) {
        if (Objects.isNull(orderId) || Objects.isNull(workId)) {
            return;
        }

        OrderChangeRecord orderChangeRecord = orderChangeRecordService.getChangeRecordByOrderId(orderId);
        if (Objects.isNull(orderChangeRecord)) {
            return;
        }
        if (NumberUtil.isNullOrZero(orderChangeRecord.getLiableMasterId())) {
            return;
        }

        // 取消责任工程师=责任工程师
        ChangeLiableMasterDTO updateChangeLiableMasterDTO = new ChangeLiableMasterDTO();
        updateChangeLiableMasterDTO.setOrderId(orderChangeRecord.getOrderId());
        updateChangeLiableMasterDTO.setLiableMasterId(orderChangeRecord.getLiableMasterId());
        updateChangeLiableMasterDTO.setLiableMasterName(orderChangeRecord.getLiableMasterName());
        orderChangeRecordService.updateCancelLiableMasterRecord(updateChangeLiableMasterDTO);
    }

}
